<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.appName"
        clearable
        placeholder="服务名称"
        style="width: 400px"
        class="filter-item"
        size="small"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="handleFilter"
      >
        查询
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <!-- <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column> -->
      <el-table-column label="服务名称" min-width="350">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="服务简介" min-width="300">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column> -->
      <el-table-column label="所属部门" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.dept }}
        </template>
      </el-table-column>
      <el-table-column label="开发人员" width="150">
        <template slot-scope="scope">
          {{ scope.row.developer }}
        </template>
      </el-table-column>
      <el-table-column label="产品经理" width="150">
        <template slot-scope="scope">
          {{ scope.row.productManager }}
        </template>
      </el-table-column>
      <el-table-column label="测试人员" width="150">
        <template slot-scope="scope">
          {{ scope.row.tester }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="160">
        <template slot-scope="scope">
          {{ scope.row.updatedTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-button type="text" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="text" size="mini"> <router-link
            :to="{ path: '/appDetail', query: { appName: row.name, activeName: 'basic' } }"
          >详情</router-link> </el-button>
          <el-button type="text" size="mini"> <router-link
            :to="{ path: '/appDetail', query: { appName: row.name, activeName: 'instance' } }"
          >实例</router-link> </el-button>
          <el-button type="text" size="mini"> <router-link
            :to="{ path: '/appDetail', query: { appName: row.name, activeName: 'rule' } }"
          >规则</router-link></el-button>
          <el-button type="text" size="mini"> <router-link
            :to="{ path: '/appDetail', query: { appName: row.name, activeName: 'monitor' } }"
          >监控</router-link></el-button>

        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchData"
    />

    <EditDetail ref="EditDetail" @updateSuccess="updateSuccessFn" />
  </div>
</template>

<script>
import { getList } from '@/api/app'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import EditDetail from './components/EditDetail.vue'

export default {
  // name: '服务列表',
  components: { Pagination, EditDetail },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        appName: ''
      },
      rules: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then((response) => {
        this.total = response.data.total
        this.list = response.data.items
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.fetchData()
    },
    handleUpdate(row) {
      this.$refs.EditDetail.openEdit(row)
    },
    handleViewInstance(row) {},
    updateSuccessFn() {
      this.fetchData()
    }
  }
}
</script>
